<?php

namespace app\interactive\admin;

use app\admin\controller\Admin;
use app\common\builder\ZBuilder;

use app\interactive\model\Active as ActiveModel;

class Active extends Admin
{

    public function Index()
    {
        $map = $this->getMap();

        $activeData = ActiveModel::where($map)->order('id asc')->paginate();

        return ZBuilder::make('table')
            ->setTableName('active')
            ->addColumns([
                ['id', 'ID'],
                ['activeName', '活动名称'],
                ['addTime', '添加时间', 'datetime', '', 'Y/m/d H:i:s'],
                ['updateTime', '更新时间', 'datetime', '', 'Y/m/d H:i:s'],
                ['right_button', '操作', 'btn']
            ])
            ->setRowList($activeData)
            ->addTopButton('add', [], true)
            ->addTopButton('delete')
            ->addRightButton('edit', [], true)
            ->addRightButton('delete')
            ->fetch();
    }

    public function add()
    {
        if ($this->request->isPost()) {
            $newActiveData = $this->request->post();
            //验证
            $result = $this->validate($newActiveData, 'Active');
            // 验证失败 输出错误信息
            if (true !== $result) {
                $this->error($result);
            }

            //验证是否已存在该活动名
            $existingActive = ActiveModel::where('activeName', $newActiveData['activeName'])->find();
            if ($existingActive) {
                $this->error('活动名称已存在，请使用其他名称');
            }

            $newActiveData['addTime'] = time();
            $newActiveData['updateTime'] = time();

            if (ActiveModel::insert($newActiveData)) {
                $this->success('新增成功', null, '_parent_reload');
            } else {
                $this->error('新增失败');
            }
        }
        return ZBuilder::make('form')
            ->setPageTitle('添加活动')
            ->addText('activeName', '活动名称', '请输入活动名称')
            ->submitConfirm()
            ->fetch();
    }

    public function edit($id = '')
    {
        if ($this->request->isPost()) {

            $newActiveData = $this->request->post();
            
            //验证
            $result = $this->validate($newActiveData, 'Active');
            // 验证失败 输出错误信息
            if (true !== $result) {
                $this->error($result);
            }
            
            //验证是否已存在该活动名
            $existingActive = ActiveModel::where('activeName', $newActiveData['activeName'])->find();
            if ($existingActive) {
                $this->error('活动名称已存在，请使用其他名称');
            }
            
            $newActiveData['updateTime'] = time();

            if (ActiveModel::where(['id' => $id])->update($newActiveData)) {
                $this->success('编辑成功', null, '_parent_reload');
            } else {
                $this->error('编辑失败');
            }
        }

        $formData = ActiveModel::find($id);

        return ZBuilder::make('form')
            ->setFormData($formData)
            ->setPageTitle('编辑活动')
            ->addText('activeName', '活动名称', '请输入新的活动名称')
            ->submitConfirm()
            ->fetch();
    }
}
